﻿using System.Windows;
using DL.PhotoCollage.Core;
using DL.PhotoCollage.Wpf.Presentation;

namespace DL.PhotoCollage.UI.Screensaver.Presentation
{
    internal class ScreensaverController : ControllerBase
    {
        private readonly string[] args;

        private readonly StartupType startupMode;

        public ScreensaverController(Application currentApplication, string[] arguments, string applicationLocalDataDirectory)
            : base(applicationLocalDataDirectory)
        {
            this.Application = currentApplication;
            this.args = arguments;
            this.ConfigurationRepository = new FileSystemConfigurationRepository(this.LocalDataDirectory);
            
            if (this.args.Length > 0)
            {
                this.startupMode = DetermineStartupType(this.args[0]);
            }

            this.LoadConfigurationSettings();
        }

        public override void StartApplication()
        {
            switch (this.startupMode)
            {
                case StartupType.Preview:
                    this.ShutdownApplication();
                    break;
                case StartupType.Config:
                    new SettingsPresenter(this);
                    break;
                default:
                    new ScreensaverPresenter(this);
                    break;
            }
        }

        private static StartupType DetermineStartupType(string arg)
        {
            string commandArg
                    = arg.ToLower(System.Globalization.CultureInfo.InvariantCulture)
                        .Trim().Substring(0, 2);

            switch (commandArg)
            {
                case "/c":
                    return StartupType.Config;
                case "/p":
                    return StartupType.Preview;
                default:
                    return StartupType.Normal;
            }
        }
    }
}
